//
//  MIneShakeViewController.m
//  GiftItems
//
//  Created by lanou3g on 15/9/23.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MIneShakeViewController.h"
#define HIEGHT [UIScreen mainScreen].bounds.size.height/2
@interface MIneShakeViewController ()
@property(nonatomic, strong) UIImageView *middleImage;
@property(nonatomic, strong) UIImageView *aboveImage;
@property(nonatomic, strong) UIImageView *underImage;
@end

@implementation MIneShakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"摇一摇";
    self.view.backgroundColor = kRGB(255.0, 230.0, 255.0);
    [self addImageView];
    
}
// 添加图片视图
- (void)addImageView {
    
    self.middleImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, HIEGHT - 100, kWidth, 200)];
    [self setImageView:self.middleImage imageName:@"bg_yaoyao_middle.png"];
    
    self.aboveImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, -50, kWidth, HIEGHT)];
    [self setImageView:self.aboveImage imageName:@"bg_yaoyao_above.png"];
    
    self.underImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, HIEGHT - 50, kWidth, HIEGHT + 50)];
    [self setImageView:self.underImage imageName:@"bg_yaoyao_under.png"];
}

// 添加图片
- (void)setImageView:(UIImageView *)imageView imageName:(NSString *)imageName {
    imageView.image = [UIImage imageNamed:imageName];
    [self.view addSubview:imageView];
}
// 设置第一响应者
- (BOOL)canBecomeFirstResponder {
    return YES;
}

// 摇动事件开始时触发
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [self animationOfShakeEvent];
}

// 设置摇一摇的动画
- (void)animationOfShakeEvent {
    [UIView animateWithDuration:1.0 animations:^{
        self.aboveImage.frame = CGRectMake(0, -100, kWidth, HIEGHT);
        self.underImage.frame = CGRectMake(0, HIEGHT + 100, kWidth, HIEGHT);
    } completion:^(BOOL finished) {
        self.aboveImage.frame = CGRectMake(0, -50, kWidth, HIEGHT);
        self.underImage.frame = CGRectMake(0, HIEGHT - 50, kWidth, HIEGHT + 50);
    }];
}

// 摇动事件结束时触发
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    // 摇一摇事件结束后,延迟执行的方法
    [self performSelector:@selector(alertMessage) withObject:nil afterDelay:1.5];
}

// 摇一摇结束后,弹出提示信息
- (void)alertMessage {
    
    WebViewController *webVC = [[WebViewController alloc] init];
    
    NSString *newUrl = [KGiftDetailsUrl stringByReplacingOccurrencesOfString:@"参数" withString:[NSString stringWithFormat:@"%u",(arc4random()%1000) + 1033700]];
    [LORequestManger GET:newUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        
        
        webVC.webUrl = dic[@"data"][@"purchase_url"];
        NSLog(@"%@",webVC.webUrl);
        webVC.type = @"热卖";
        [self presentViewController:webVC animated:YES completion:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];

    
}

// 摇动事件取消时触发
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"摇晃时间取消时触发");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
